provider "alicloud" {
  region                = local.region
}

terraform {
  required_version      = "~> 0.12"  
  backend "oss" {
    bucket              = "deeproute-terraform-resources"
    key                 = "prod/services/prod-ack-operator-cluster"
    tablestore_endpoint = "https://terraform-status.cn-shenzhen.ots.aliyuncs.com"
    tablestore_table    = "terraform_statelock_table"    
    region              = "cn-shenzhen"
  }
}

locals {
  vpc                   = "vpc-wz9v3pmnqrr7z6xjg0p1o"
}

// Add nat gateway access pod access internal
resource "alicloud_nat_gateway" "main" { //按量付费
  name          = local.fullname
  nat_type      = "Enhanced"
  vpc_id        = local.vpc
  vswitch_id    = "vsw-wz9b6w5009d0noscpmcta"
}

resource "alicloud_eip_address" "main" { //按量付费
  address_name          = local.fullname
  bandwidth             = 7
  resource_group_id     = "rg-aek2tc4weboko6i"
}

resource "alicloud_eip_association" "main" {
  allocation_id = alicloud_eip_address.main.id
  instance_id   = alicloud_nat_gateway.main.id
}

// add [node] vswitch to snat,accept node access Internet
resource "alicloud_snat_entry" "vswitch-prod-node-vsw-shenzhen-d" {
  snat_table_id     = alicloud_nat_gateway.main.snat_table_ids
  source_vswitch_id = "vsw-wz9b6w5009d0noscpmcta"
  snat_ip           = alicloud_eip_address.main.ip_address
  depends_on        = [alicloud_eip_association.main]
}

resource "alicloud_snat_entry" "vswitch-prod-node-vsw-shenzhen-e" {
  snat_table_id     = alicloud_nat_gateway.main.snat_table_ids
  source_vswitch_id = "vsw-wz9kqy4946bndzke1ejci"
  snat_ip           = alicloud_eip_address.main.ip_address
  depends_on        = [alicloud_eip_association.main]
}

// add [pod] vswitch to snat,accept pod access Internet
resource "alicloud_snat_entry" "vswitch-prod-pod-vsw-shenzhen-d" {
  snat_table_id     = alicloud_nat_gateway.main.snat_table_ids
  source_vswitch_id = "vsw-wz9br1fw5vmm3wx1ksl9t"
  snat_ip           = alicloud_eip_address.main.ip_address
  depends_on        = [alicloud_eip_association.main]
}

resource "alicloud_snat_entry" "vswitch-prod-pod-vsw-shenzhen-e" {
  snat_table_id     = alicloud_nat_gateway.main.snat_table_ids
  source_vswitch_id = "vsw-wz9u4qhj4sl7pmnk6vrol"
  snat_ip           = alicloud_eip_address.main.ip_address
  depends_on        = [alicloud_eip_association.main]
}

data "alicloud_instance_types" "c7a-xlarge" {
  cpu_core_count               = 4
  memory_size                  = 8
  instance_type_family         = "ecs.c7a"
  kubernetes_node_role         = "Worker"
}

data "alicloud_instance_types" "r6-xlarge" {
  cpu_core_count               = 4
  memory_size                  = 32
  instance_type_family         = "ecs.r6"
  kubernetes_node_role         = "Worker"
}

data "alicloud_instance_types" "r6-2xlarge" {
  cpu_core_count               = 8
  memory_size                  = 64
  instance_type_family         = "ecs.r6"
  kubernetes_node_role         = "Worker"
}

resource "alicloud_cs_managed_kubernetes" "main" {
  name                          = "prod-ack-operation-cluster"
  count                         = 1
  version                       = "1.22.3-aliyun.1"
  timezone                      = "Asia/Shanghai"
  resource_group_id             = "rg-aek2tc4weboko6i"
  worker_vswitch_ids            = ["vsw-wz9b6w5009d0noscpmcta", "vsw-wz9kqy4946bndzke1ejci"]
  pod_vswitch_ids               = ["vsw-wz9u4qhj4sl7pmnk6vrol", "vsw-wz9br1fw5vmm3wx1ksl9t"]
  node_cidr_mask                = "27"  #per node 62 pods, cluster availabel scale to 16 nodes
  # install_cloud_monitor         = true
  proxy_mode                    = "ipvs"
  service_cidr                  = "10.244.208.0/21"
  cluster_spec                  = "ack.pro.small" #"ack.standard" #"ack.pro.small"
  new_nat_gateway               = false
  is_enterprise_security_group  = true
  # load_balancer_spec            = "slb.s1.small"
  slb_internet_enabled          = true
  password                      = "Just4Test"
  worker_disk_category          = "cloud_efficiency"
  cpu_policy                    = "none"

  dynamic "addons" {
      for_each = var.cluster_addons
      content {
        name                    = lookup(addons.value, "name", var.cluster_addons)
        config                  = lookup(addons.value, "config", var.cluster_addons)
        disabled                = lookup(addons.value, "disabled", var.cluster_addons)
      }
  }
  runtime = {
    name    = "docker"
    version = "19.03.15"
  }
  maintenance_window {
    enable            = false
    maintenance_time  = "01:00:00Z"
    duration          = "3h"
    weekly_period     = "Monday,Friday"
  }
  tags                = local.tags
}

# NodePoolName-v{a}-v{b}-v{c}
#   a: cluster version
#   b: k8s version
#   c: node group version

resource "alicloud_cs_kubernetes_node_pool" "np1" {
  cluster_id                   = alicloud_cs_managed_kubernetes.main.0.id
  name                         = "public-worker-node-v1-v1-22-v1"
  vswitch_ids                  = ["vsw-wz9b6w5009d0noscpmcta", "vsw-wz9kqy4946bndzke1ejci"]
  instance_types               = [data.alicloud_instance_types.c7a-xlarge.instance_types.0.id]
  image_id                     = "aliyun_3_x64_20G_alibase_20220225.vhd"   //https://help.aliyun.com/document_detail/173054.html?spm=a2c4g.11186623.6.581.6c0571c8KnnMZV
  resource_group_id            = "rg-aek2tc4weboko6i"
  password                     = "Just4Test"
  system_disk_category         = "cloud_essd"
  system_disk_size             = "80"
  install_cloud_monitor        = true
  instance_charge_type         = "PostPaid"
  # # use PrePaid
  # instance_charge_type         = "PrePaid"
  # period                       = 1
  # period_unit                  = "Month"
  # auto_renew                   = true
  # auto_renew_period            = 1
  desired_size                  = 7 //which can be zero
  management {
    auto_repair       = false
    auto_upgrade      = false
    max_unavailable   = 1
  } 
  # # With auto-scaling is enabled, the nodes in the node pool will be labeled with `k8s.aliyun.com=true` to prevent system pods such as coredns, metrics-servers from being scheduled to elastic nodes, and to prevent node shrinkage from causing business abnormalities.
  # scaling_config {
  #   min_size          = 2
  #   max_size          = 10
  #   is_bond_eip       = false
  # }
  labels {
    key               = "env"
    value             = "production-ack"
  }
  labels {
    key               = "cluster"
    value             = "prod-ack-operation-cluster"
  }
  labels {
    key               = "nodePool"
    value             = "public-worker-node-v1-v1-22-v1"
  }
  tags                = local.tags
}

resource "alicloud_cs_kubernetes_node_pool" "np3" {
  cluster_id                   = alicloud_cs_managed_kubernetes.main.0.id
  name                         = "public-worker-node-v1-v1-22-v3"
  vswitch_ids                  = ["vsw-wz9b6w5009d0noscpmcta", "vsw-wz9kqy4946bndzke1ejci"]
  instance_types               = [data.alicloud_instance_types.r6-2xlarge.instance_types.0.id]
  image_id                     = "aliyun_3_x64_20G_alibase_20220225.vhd"   //https://help.aliyun.com/document_detail/173054.html?spm=a2c4g.11186623.6.581.6c0571c8KnnMZV
  resource_group_id            = "rg-aek2tc4weboko6i"
  password                     = "Just4Test"
  system_disk_category         = "cloud_ssd"
  system_disk_size             = "80"
  install_cloud_monitor        = true
  instance_charge_type         = "PostPaid"
  # # use PrePaid
  # instance_charge_type         = "PrePaid"
  # period                       = 1
  # period_unit                  = "Month"
  # auto_renew                   = true
  # auto_renew_period            = 1
  desired_size                   = 1 //which can be zero
  management {
    auto_repair       = false
    auto_upgrade      = false
    max_unavailable   = 1
  } 
  # # With auto-scaling is enabled, the nodes in the node pool will be labeled with `k8s.aliyun.com=true` to prevent system pods such as coredns, metrics-servers from being scheduled to elastic nodes, and to prevent node shrinkage from causing business abnormalities.
  # scaling_config {
  #   min_size          = 2
  #   max_size          = 10
  #   is_bond_eip       = false
  # }
  labels {
    key               = "env"
    value             = "production-ack"
  }
  labels {
    key               = "cluster"
    value             = "prod-ack-operation-cluster"
  }
  labels {
    key               = "nodePool"
    value             = "public-worker-node-v1-v1-22-v3"
  }
  tags                = local.tags
}